/* 
* Copyright (c) 2009, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using JulMar.Windows.Mvvm;
using Bio.Data.Interfaces;

namespace Bio.Views.Alignment.ViewModels
{
    /// <summary>
    /// This class represents a single cell visible on the screen
    /// </summary>
    class CellViewModel : ViewModel
    {
        private static readonly object _cellSelectedProperty = new object();
        private const string _cellSelectedPropertyKey = "_CVMSel";

        /// <summary>
        /// Column index for this cell
        /// </summary>
        public int Column { get; private set; }

        /// <summary>
        /// Row parent
        /// </summary>
        public RowViewModel ParentRow { get; private set; }

        /// <summary>
        /// Current "value" of the cell
        /// </summary>
        private IBioSymbol _value;
        public IBioSymbol Value
        {
            get { return _value; }
            set
            {
                _value = value;
                OnPropertyChanged("Value");
            }
        }

        /// <summary>
        /// True/false if this cell is selected; we require that the underlying IBioSymbl supports
        /// </summary>
        public bool IsSelected
        {
            get
            {
                if (_value == null) 
                    return false;

                var iexp = _value as IExtensibleProperties;
                return (iexp != null)
                           ? iexp.DoesExtendedPropertyExist(_cellSelectedPropertyKey)
                           : false;
            }
            set
            {
                var iexp = _value as IExtensibleProperties;
                if (iexp != null)
                {
                    if (value)
                        iexp.SetExtendedProperty(_cellSelectedPropertyKey, _cellSelectedProperty);
                    else
                        iexp.ClearExtendedProperty(_cellSelectedPropertyKey);
                    OnPropertyChanged("IsSelected");
                }
            }
        }

        /// <summary>
        /// True/False if this cell matches the selected reference sequence at this position
        /// </summary>
        private string _backgroundBrush;
        public string BackgroundBrush
        {
            get { return string.IsNullOrEmpty(_backgroundBrush) ? "Transparent" : _backgroundBrush; }
            set { _backgroundBrush = value; OnPropertyChanged("BackgroundBrush"); }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="rowParent"></param>
        /// <param name="col"></param>
        public CellViewModel(RowViewModel rowParent, int col)
        {
            ParentRow = rowParent;
            Column = col;
        }

        /// <summary>
        /// This method is used to update the cell in a batch - all properties
        /// are changed and then a single PropertyChange notification is raised
        /// </summary>
        /// <param name="bioSymbol">New symbol</param>
        /// <param name="backgroundBrush"></param>
        internal void Update(IBioSymbol bioSymbol, string backgroundBrush)
        {
            _value = bioSymbol;
            _backgroundBrush = backgroundBrush;
            OnPropertyChanged(null);
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return string.Format("Col:{0} IsSelected={1}, Value={2}", Column, IsSelected, Value);
        }
    }
}